package cc.noau.coder.main;

import cc.noau.coder.bean.CoderSetting;
import cc.noau.coder.bean.SettingRow;
import cc.noau.coder.bean.TableRow;
import cc.noau.coder.bean.UserSetting;
import cc.noau.coder.db.DBConnector;
import cc.noau.coder.generator.GenericCodeGenerator;

import java.util.*;

/**
 * 代码生成器入口
 */
public class CodeGeneratePlugins {

    private CoderSetting setting;

    public CodeGeneratePlugins() {
        this.setting = new CoderSetting();
    }

    public CodeGeneratePlugins(String configFile) {
        this.setting = new CoderSetting(configFile);
    }

    /**
     * 测试数据库连接
     *
     * @return
     * @throws Exception
     */
    public Boolean testConnection() throws Exception {
        return DBConnector.testDB(setting);
    }

    /**
     * @param tableRow
     * @param infoMap  thePackage
     *                 generateFileType
     * @throws Exception
     */
    @Deprecated
    public Boolean generate(TableRow tableRow, Map<String, Object> infoMap) {
        return this.generate(new ArrayList<>(Arrays.asList(tableRow)), infoMap);
    }

    /**
     * @param tableRows
     * @param infoMap   thePackage
     *                  generateFileType
     * @throws Exception
     */
    @Deprecated
    public Boolean generate(List<TableRow> tableRows, Map<String, Object> infoMap) {
        try {
            if(DBConnector.testDB(setting)) {
                List<SettingRow> settingRows = new ArrayList<>();
                //数据库表
                for (TableRow tableRow : tableRows) {
                    SettingRow settingRow = new SettingRow();
                    settingRow.setModule(tableRow.getModule());
                    settingRow.setTableName(tableRow.getTableName());
                    settingRow.setDomainObjectName(tableRow.getDomainObjectName());
                    settingRow.setUseGeneratedKeys(tableRow.getUseGeneratedKeys());
                    settingRows.add(settingRow);
                }
                if (infoMap != null) {
                    if (infoMap.get("thePackage") != null) {
                        setting.setThePackage(infoMap.get("thePackage").toString());
                    }
                    if (infoMap.get("generateFileType") != null) {
                        setting.setGenerateFileType(infoMap.get("generateFileType").toString());
                    }
                    if (infoMap.get("author") != null) {
                        setting.setAuthor(infoMap.get("author").toString());
                    }
                    if (infoMap.get("version") != null) {
                        setting.setVersion(infoMap.get("version").toString());
                    }
                }
                setting.setSettingRows(settingRows);

                // 代码生成
                GenericCodeGenerator codeGenerator = new GenericCodeGenerator(setting);
                codeGenerator.generate();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     *
     * @param tableRow
     * @param userSetting
     * @return
     */
    public Boolean generate(TableRow tableRow, UserSetting userSetting) {
        return this.generate(new ArrayList<>(Arrays.asList(tableRow)), userSetting);
    }

    /**
     *
     * @param tableRows
     * @param userSetting
     * @return
     */
    public Boolean generate(List<TableRow> tableRows, UserSetting userSetting) {
        try {
            if(DBConnector.testDB(setting)) {
                List<SettingRow> settingRows = new ArrayList<>();
                //数据库表
                for (TableRow tableRow : tableRows) {
                    SettingRow settingRow = new SettingRow();
                    settingRow.setModule(tableRow.getModule());
                    settingRow.setTableName(tableRow.getTableName());
                    settingRow.setDomainObjectName(tableRow.getDomainObjectName());
                    settingRow.setUseGeneratedKeys(tableRow.getUseGeneratedKeys());
                    settingRows.add(settingRow);
                }
                if (userSetting != null) {
                    if (userSetting.getUrl() != null) {
                        setting.setUrl(userSetting.getUrl());
                    }
                    if (userSetting.getUsername() != null) {
                        setting.setAccount(userSetting.getUsername());
                    }
                    if (userSetting.getPassword() != null) {
                        setting.setPassword(userSetting.getPassword());
                    }
                    if (userSetting.getTemplatePath() != null) {
                        setting.setTemplatePath(userSetting.getTemplatePath());
                    }

                    if (userSetting.getThePackage() != null) {
                        setting.setThePackage(userSetting.getThePackage());
                    }
                    if (userSetting.getGenerateFileType() != null) {
                        setting.setGenerateFileType(userSetting.getGenerateFileType());
                    }
                    if (userSetting.getAuthor() != null) {
                        setting.setAuthor(userSetting.getAuthor());
                    }
                    if (userSetting.getVersion() != null) {
                        setting.setVersion(userSetting.getVersion());
                    }
                }
                setting.setSettingRows(settingRows);

                // 代码生成
                GenericCodeGenerator codeGenerator = new GenericCodeGenerator(setting);
                codeGenerator.generate();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
